package cn.iocoder.yudao.module.infra.service.oss;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.string.StringUtils;
import cn.iocoder.yudao.module.infra.oss.entity.UploadResult;
import cn.iocoder.yudao.module.infra.oss.factory.OssFactory;
import cn.iocoder.yudao.module.infra.oss.service.IOssStrategy;
import cn.iocoder.yudao.module.infra.controller.admin.oss.vo.OssPageReqVO;
import cn.iocoder.yudao.module.infra.dal.dataobject.oss.OssDO;
import cn.iocoder.yudao.module.infra.dal.mysql.oss.OssMapper;
import com.aliyun.oss.ServiceException;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Base64;
import java.util.Collection;
import java.util.List;

/**
 * oss Service 实现类
 *
 * @author guangpu
 */
@Service
public class OssServiceImpl implements OssService{
    @Resource
    OssMapper ossMapper;

    @Override
    public PageResult<OssDO> getFilePage(OssPageReqVO pageReqVO) {
        return ossMapper.selectPage(pageReqVO);
    }

    @Override
    public OssDO upload(MultipartFile file) {
        String originalfileName = file.getOriginalFilename();
        String suffix = StringUtils.substring(originalfileName, originalfileName.lastIndexOf("."), originalfileName.length());
        IOssStrategy storage = OssFactory.instance();
        UploadResult uploadResult;
        try {
            uploadResult = storage.uploadSuffix(file.getBytes(), suffix, file.getContentType());
        } catch (IOException e) {
            throw new ServiceException(e.getMessage());
        }
        // 保存文件信息
        OssDO oss = new OssDO();
        oss.setUrl(uploadResult.getUrl());
        oss.setFileSuffix(suffix);
        oss.setFileName(uploadResult.getFilename());
        oss.setOriginalName(originalfileName);
        oss.setService(storage.getServiceType().getValue());
        ossMapper.insert(oss);
        return oss;
    }

    @Override
    public OssDO upload(String ImageData) {

        byte[]  file=Base64.getDecoder().decode(ImageData);
        String mimeType="image/png";
        String suffix = ".png";
        IOssStrategy storage = OssFactory.instance();
        UploadResult uploadResult;
        uploadResult = storage.uploadSuffix(file, suffix, mimeType);
        // 保存文件信息
        OssDO oss = new OssDO();
        oss.setUrl(uploadResult.getUrl());
        oss.setFileSuffix(suffix);
        oss.setFileName(uploadResult.getFilename());
        oss.setOriginalName("signature");
        oss.setService(storage.getServiceType().getValue());
        ossMapper.insert(oss);
        return oss;
    }

    @Override
    public OssDO getById(Long id) {
        return ossMapper.selectById(id);
    }

    @Override
    public void deleteFile(Collection<Long> ids) {
        List<OssDO> ossDOS = ossMapper.selectBatchIds(ids);
        for(OssDO ossDO : ossDOS) {
            IOssStrategy storage = OssFactory.instance(ossDO.getService());
            storage.delete(ossDO.getUrl());
            // 更新
            ossMapper.deleteById(ossDO.getId());
        }
    }

    @Override
    public List<OssDO> selectByIds(Collection<String> ids) {
        return ossMapper.selectByIds(ids);
    }
}
